package javax.activation;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import myjava.awt.datatransfer.DataFlavor;

public class DataHandler {
    private static final DataFlavor[] emptyFlavors = new DataFlavor[0];
    private static DataContentHandlerFactory factory = null;
    private CommandMap currentCommandMap = null;
    private DataContentHandler dataContentHandler = null;
    private DataSource dataSource = null;
    private DataContentHandler factoryDCH = null;
    private DataSource objDataSource = null;
    private Object object = null;
    private String objectMimeType = null;
    private DataContentHandlerFactory oldFactory = null;
    private String shortType = null;
    private DataFlavor[] transferFlavors = emptyFlavors;

    public DataHandler(DataSource ds) {
        this.dataSource = ds;
        this.oldFactory = factory;
    }

    public DataHandler(Object obj, String mimeType) {
        this.object = obj;
        this.objectMimeType = mimeType;
        this.oldFactory = factory;
    }

    private synchronized CommandMap getCommandMap() {
        CommandMap commandMap;
        if (this.currentCommandMap != null) {
            commandMap = this.currentCommandMap;
        } else {
            commandMap = CommandMap.getDefaultCommandMap();
        }
        return commandMap;
    }

    public DataSource getDataSource() {
        if (this.dataSource != null) {
            return this.dataSource;
        }
        if (this.objDataSource == null) {
            this.objDataSource = new DataHandlerDataSource(this);
        }
        return this.objDataSource;
    }

    public String getName() {
        if (this.dataSource != null) {
            return this.dataSource.getName();
        }
        return null;
    }

    public String getContentType() {
        if (this.dataSource != null) {
            return this.dataSource.getContentType();
        }
        return this.objectMimeType;
    }

    public InputStream getInputStream() throws IOException {
        if (this.dataSource != null) {
            return this.dataSource.getInputStream();
        }
        DataContentHandler dch = getDataContentHandler();
        if (dch == null) {
            throw new UnsupportedDataTypeException("no DCH for MIME type " + getBaseType());
        } else if ((dch instanceof ObjectDataContentHandler) && ((ObjectDataContentHandler) dch).getDCH() == null) {
            throw new UnsupportedDataTypeException("no object DCH for MIME type " + getBaseType());
        } else {
            final DataContentHandler fdch = dch;
            final PipedOutputStream pos = new PipedOutputStream();
            InputStream pin = new PipedInputStream(pos);
            new Thread(new Runnable() {
                /* JADX WARNING: inconsistent code. */
                /* Code decompiled incorrectly, please refer to instructions dump. */
                public void run() {
                    /*
                    r4 = this;
                    r0 = r1;	 Catch:{ IOException -> 0x0019, all -> 0x0022 }
                    r1 = javax.activation.DataHandler.this;	 Catch:{ IOException -> 0x0019, all -> 0x0022 }
                    r1 = r1.object;	 Catch:{ IOException -> 0x0019, all -> 0x0022 }
                    r2 = javax.activation.DataHandler.this;	 Catch:{ IOException -> 0x0019, all -> 0x0022 }
                    r2 = r2.objectMimeType;	 Catch:{ IOException -> 0x0019, all -> 0x0022 }
                    r3 = r4;	 Catch:{ IOException -> 0x0019, all -> 0x0022 }
                    r0.writeTo(r1, r2, r3);	 Catch:{ IOException -> 0x0019, all -> 0x0022 }
                    r0 = r4;	 Catch:{ IOException -> 0x0029 }
                    r0.close();	 Catch:{ IOException -> 0x0029 }
                L_0x0018:
                    return;
                L_0x0019:
                    r0 = move-exception;
                    r0 = r4;	 Catch:{ IOException -> 0x0020 }
                    r0.close();	 Catch:{ IOException -> 0x0020 }
                    goto L_0x0018;
                L_0x0020:
                    r0 = move-exception;
                    goto L_0x0018;
                L_0x0022:
                    r0 = move-exception;
                    r1 = r4;	 Catch:{ IOException -> 0x002b }
                    r1.close();	 Catch:{ IOException -> 0x002b }
                L_0x0028:
                    throw r0;
                L_0x0029:
                    r0 = move-exception;
                    goto L_0x0018;
                L_0x002b:
                    r1 = move-exception;
                    goto L_0x0028;
                    */
                    throw new UnsupportedOperationException("Method not decompiled: javax.activation.DataHandler.1.run():void");
                }
            }, "DataHandler.getInputStream").start();
            return pin;
        }
    }

    public void writeTo(OutputStream os) throws IOException {
        if (this.dataSource != null) {
            byte[] data = new byte[8192];
            InputStream is = this.dataSource.getInputStream();
            while (true) {
                int bytes_read = is.read(data);
                if (bytes_read <= 0) {
                    break;
                }
                try {
                    os.write(data, 0, bytes_read);
                } finally {
                    is.close();
                }
            }
            return;
        }
        getDataContentHandler().writeTo(this.object, this.objectMimeType, os);
    }

    public Object getContent() throws IOException {
        if (this.object != null) {
            return this.object;
        }
        return getDataContentHandler().getContent(getDataSource());
    }

    private synchronized DataContentHandler getDataContentHandler() {
        DataContentHandler dataContentHandler;
        if (factory != this.oldFactory) {
            this.oldFactory = factory;
            this.factoryDCH = null;
            this.dataContentHandler = null;
            this.transferFlavors = emptyFlavors;
        }
        if (this.dataContentHandler != null) {
            dataContentHandler = this.dataContentHandler;
        } else {
            String simpleMT = getBaseType();
            if (this.factoryDCH == null && factory != null) {
                this.factoryDCH = factory.createDataContentHandler(simpleMT);
            }
            if (this.factoryDCH != null) {
                this.dataContentHandler = this.factoryDCH;
            }
            if (this.dataContentHandler == null) {
                if (this.dataSource != null) {
                    this.dataContentHandler = getCommandMap().createDataContentHandler(simpleMT, this.dataSource);
                } else {
                    this.dataContentHandler = getCommandMap().createDataContentHandler(simpleMT);
                }
            }
            if (this.dataSource != null) {
                this.dataContentHandler = new DataSourceDataContentHandler(this.dataContentHandler, this.dataSource);
            } else {
                this.dataContentHandler = new ObjectDataContentHandler(this.dataContentHandler, this.object, this.objectMimeType);
            }
            dataContentHandler = this.dataContentHandler;
        }
        return dataContentHandler;
    }

    private synchronized String getBaseType() {
        if (this.shortType == null) {
            String ct = getContentType();
            try {
                this.shortType = new MimeType(ct).getBaseType();
            } catch (MimeTypeParseException e) {
                this.shortType = ct;
            }
        }
        return this.shortType;
    }
}
